<template>
	<van-nav-bar title="确认订单" left-text="" left-arrow @click-left="goBack" />

	<!-- 有默认地址-->
	<van-contact-card
		v-if="JSON.stringify(address) != '{}'"
		type="edit"
		:tel="address.mobile"
		:name="address.consignee"
		@click="toAddressPage"
	/>
	<!-- 需要到地址列表选择地址-->
	<van-contact-card type="add" @click="toAddressPage" v-else />
	<van-card
		v-if="!buychecked"
		v-for="item in cartlist"
		:key="item.id"
		:num="item.nums"
		:price="item.total"
		:desc="'库存：' + item.product.stock"
		:title="item.product.name"
		:thumb="item.product.thumbs_text"
	>
		<template #tags>
			<div>单价： {{ item.price }}</div>
		</template>
	</van-card>
	<van-card
		v-else
		:key="product.id"
		:num="1"
		:price="product.price"
		:desc="'库存：' + product.stock"
		:title="product.name"
		:thumb="product.thumbs_text"
	>
		<template #tags>
			<div>单价： {{ product.price }}</div>
		</template>
	</van-card>
	<van-cell-group inset>
		<van-field
			v-model="remark"
			rows="2"
			autosize
			label="订单备注"
			type="textarea"
			maxlength="50"
			placeholder="请输入订单备注"
			show-word-limit
		/>
	</van-cell-group>
	<van-submit-bar :price="total" button-text="提交订单" @submit="onSubmit" />
	<van-dialog
		v-model:show="show"
		title="立即支付"
		show-cancel-button
		confirmButtonText="立即支付"
		@confirm="onpayment"
		@cancel="topage"
	>
		<van-field
			is-link
			readonly
			label="支付方式"
			v-model="test"
			placeholder="请选择支付方式"
			@click="show1 = true"
		/>
		<van-popup v-model:show="show1" round position="bottom">
			<van-cascader
				v-model="payment"
				title="支付方式"
				:options="way"
				@close="show1 = false"
				@finish="show1 = false"
				@change="onChange"
			/>
		</van-popup>
		<div class="way">是否继续支付金额</div>
	</van-dialog>
</template>
<script setup>
	const router = useRouter();
	const route = useRoute();
	const goBack = () => {
		history.back();
	};
	const proid = ref(route.query.proid ?? 0);
	const orderid = ref(0);
	const buychecked = ref(route.query.buychecked ?? false);
	const product = ref({});
	const cartlist = ref([]);
	const cartids = ref(route.query.cartids ?? 0);
	const { cookies } = useCookies();
	const LoginUser = reactive(cookies.get("LoginUser"));
	const remark = ref("");
	const address = ref(cookies.get("Address") ?? {});
	const show = ref(false);
	const show1 = ref(false);
	const test = ref("余额");
	const way = [
		{ text: "余额", value: 2 },
		{ text: "微信", value: 0 },
		{ text: "支付宝", value: 1 },
	];
	const payment = ref(2);
	// 计算总价
	const total = computed(() => {
		let sum = 0;
		// 计算选中
		if (buychecked.value) {
			return product.value.price * 100;
		}
		sum = cartlist.value.reduce(
			(accumulator, currentValue) => accumulator + parseFloat(currentValue.total),
			0
		);
		return sum * 100;
	});
	onBeforeMount(() => {
		if (buychecked.value) {
			getProDate();
		} else {
			getCartData();
		}

		if (JSON.stringify(address) != "{}" && !cookies.get("Address")) {
			getAddressData();
		}
	});
	// 获取购物车数据
	const getCartData = async () => {
		let result = await Api.product.cartIndex({
			busid: LoginUser.id,
			cartid: cartids.value,
		});
		if (result.code == 1) {
			cartlist.value = result.data;
		}
	};
	// 获取商品数据
	const getProDate = async () => {
		let result = await Api.product.productInfo({
			proid: proid.value,
			busid: LoginUser.id,
		});
		if (result.code == 1) {
			product.value = result.data.info;
		}
	};
	// 获取默认地址
	const getAddressData = async () => {
		let result = await Api.business.addressOrder({
			busid: LoginUser.id,
		});
		if (result.code == 1) {
			address.value = result.data;
		}
	};
	const toAddressPage = () => {
		router.push({
			path: "/business/address/index",
			query: {
				action: "order",
			},
		});
	};
	const onSubmit = async () => {
		let data = {
			busid: LoginUser.id,
			amount: total.value / 100,
			remark: remark.value,
			addressid: address.value.id,
		};
		let result;
		if (buychecked.value) {
			data.proid = proid.value;
			result = await Api.business.orderbuy(data);
			if (result.code == 1) {
				orderid.value = result.data;
				showSuccessToast({
					message: result.msg,
					onClose: function () {
						show.value = true;
					},
				});
			} else {
				showFailToast(result.msg);
			}
		} else {
			data.cartid = cartids.value;
			result = await Api.business.orderAdd(data);
			if (result.code == 1) {
				showSuccessToast({
					message: result.msg,
					onClose: function () {
						router.push("/business/order/index");
					},
				});
			} else {
				showFailToast(result.msg);
			}
		}
	};
	const onpayment = async () => {
		let result = await Api.business.orderpayment({
			orderid: orderid.value,
			busid: LoginUser.id,
			way: payment.value,
		});
		if (result.code == 1) {
			showSuccessToast({
				message: result.msg,
				onClose: function () {
					router.push("/business/order/index");
				},
			});
		} else {
			showFailToast(result.msg);
			router.push("/business/order/index");
		}
	};
	const onChange = ({ value }) => {
		way.forEach((item) => {
			if (item.value == value) {
				test.value = item.text;
			}
		});
	};
	const topage = () => {
		router.push("/business/order/index");
	};
</script>
<style lang="scss" scoped>
	.van-dialog {
		height: 50%;
	}
	.way {
		padding: 40px 10px 20px;
		text-align: center;
		font-size: 20px;
		color: #b01f1f;
	}
</style>